(function () {
'use strict';
    layui.use(["jquery","form","layer"],function(){
        var form = layui.form;
        var $ = layui.$;
        var layer = layui.layer;
        //校验是否已登录
        var managerId = getCookie("managerId");
        //因为影响锁屏功能,去除已登录验证功能 20180731
        // if(!isNull(managerId)){
        //     layer.msg("您已登录，正在跳转至首页",{icon:6});
        //     setTimeout(function () {
        //         window.location.href = "/CivilAffairs/manager/index.html";
        //     },500);
        // }

        //自定义验证规则
        form.verify({
            username: function (value) {
                if (value == "") {
                    return "请输入用户名";
                }
            },
            password: function (value) {
                if (value == "") {
                    return "请输入密码";
                }
            },
            code: function (value) {
                if (value == "") {
                    return "请输入验证码";
                }
            }
        });

        //监听提交
        form.on('submit(login)', function(data){
            var index = layer.load(3);
            //console.log(data.field);
            $.ajax({
                url : "/CivilAffairs/manager/login",
                type : "post",
                //async : false,
                data: data.field,
                dataType : "json",
                success : function(result) {
                    layer.close(index);
                    if(checkResult(result)){
                        layer.msg("欢迎登陆", { icon: 6 });
                        var manager = result.data;
                        addCookie("managerId",manager.manager_id);
                        addCookie("managerName",manager.real_name);
                        addCookie("username",manager.username);
                        addCookie("managerLevel",manager.manager_level);
                        addCookie("managerFacePic",manager.user_face_pic);
                        setTimeout(function () {
                            window.location.href = "/CivilAffairs/manager/index.html";
                        },10);

                    }else{
                        //刷新验证码
                        $(".layadmin-user-login-codeimg").click();
                    }
                },
                error : function(result) {
                    layer.close(index);
                    console.log("登录验证异常:"+result.status+","+result.statusMessage);
                    layer.msg("登陆异常,请稍后重试", { icon: 2 });
                }
            });
            //不执行form提交
            return false;
        });
    });

}());

//刷新验证码
function getImage() {
    $.ajax({
        url : "/CivilAffairs/manager/getImgCode",
        type : "post",
        dataType : "json",
        success : function(result) {
            if(checkResult(result)){

            }
        },
        error : function(result) {
            console.log("登录验证异常:"+result.status+","+result.statusMessage);
            layer.msg("登陆异常,请稍后重试", { icon: 2 });
        }
    });
}

